package com.mccree.handler;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelPipeline;
import io.netty.util.CharsetUtil;

import java.util.concurrent.TimeUnit;

public class NettyServerHandler extends ChannelInboundHandlerAdapter {


    //读取数据
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        //解决方案一：用户自定义的普通任务

//        ctx.channel().eventLoop().execute(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    Thread.sleep(5000);
//                    ctx.writeAndFlush(Unpooled.copiedBuffer("hello, 客户端~(>^ω^<)喵2", CharsetUtil.UTF_8));
//                    System.out.println("channel code:"+ctx.channel().hashCode());
//                } catch (InterruptedException e) {
//                    System.out.println("发生异常"+e.getMessage());
//                }
//            }
//        });
//        ctx.channel().eventLoop().execute(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    Thread.sleep(5000);
//                    ctx.writeAndFlush(Unpooled.copiedBuffer("hello, 客户端~(>^ω^<)喵3", CharsetUtil.UTF_8));
//                    System.out.println("channel code:"+ctx.channel().hashCode());
//                } catch (InterruptedException e) {
//                    System.out.println("发生异常"+e.getMessage());
//                }
//            }
//        });

        //解决方案2：用户自定义定时任务
        ctx.channel().eventLoop().schedule(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(5000);
                    ctx.writeAndFlush(Unpooled.copiedBuffer("hello, 客户端~(>^ω^<)喵4", CharsetUtil.UTF_8));
                    System.out.println("channel code=" + ctx.channel().hashCode());
                }catch (Exception e){
                    System.out.println("发生异常"+e.getMessage());
                }
            }
        },5, TimeUnit.SECONDS);
        System.out.println("go on ...");


//        System.out.println("服务器读取线程:"+Thread.currentThread().getName()+";channel:"+ctx.channel());
//        System.out.println("Server ctx = "+ctx);
//        System.out.println("看看channel和pipeline的关系");
//        Channel channel = ctx.channel();
//        ChannelPipeline pipeline = ctx.pipeline();
//
//        ByteBuf byteBuf = (ByteBuf) msg;
//        System.out.println("客户端发送消息是:"+byteBuf.toString(CharsetUtil.UTF_8));
//        System.out.println("客户端地址:"+channel.remoteAddress());
    }

    //数据读取完毕
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.writeAndFlush(Unpooled.copiedBuffer("hello!客户端~(>^ω^<)喵",CharsetUtil.UTF_8));
    }

    //处理异常
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
        cause.printStackTrace();
    }
}
